﻿using System;
using System.IO;

namespace Llkd
{
    class ZdsLog
    {
        public static void Del_Log()
        {
            string log_folder = "r:\\";

            DirectoryInfo dirInfo = new DirectoryInfo(log_folder);
            if (dirInfo.Exists)
            {

            }
            else
            {
                log_folder = AppDomain.CurrentDomain.BaseDirectory + "log\\";
            }

            log_folder = log_folder.Replace("\\\\", "\\");

            string strLogFullPathName5 = log_folder + "log5.txt";
            FileInfo fiLog5 = new FileInfo(strLogFullPathName5);
            if (fiLog5.Exists) ////验证文件是否存在，有则 删除
            {
                fiLog5.Delete();
            }

            string strLogFullPathName4 = log_folder + "log4.txt";
            FileInfo fiLog4 = new FileInfo(strLogFullPathName4);
            if (fiLog4.Exists) ////验证文件是否存在，有则 删除
            {
                fiLog4.Delete();
            }

            string strLogFullPathName3 = log_folder + "log3.txt";
            FileInfo fiLog3 = new FileInfo(strLogFullPathName3);
            if (fiLog3.Exists) ////验证文件是否存在，有则 删除
            {
                fiLog3.Delete();
            }

            string strLogFullPathName2 = log_folder + "log2.txt";
            FileInfo fiLog2 = new FileInfo(strLogFullPathName2);
            if (fiLog2.Exists) ////验证文件是否存在，有则 删除
            {
                fiLog2.Delete();
            }

            string strLogFullPathName1 = log_folder + "log1.txt";
            FileInfo fiLog1 = new FileInfo(strLogFullPathName1);
            if (fiLog1.Exists)   ////验证文件是否存在，有则 删除
            {
                fiLog1.Delete();
            }
        }

        public static void Init_log()
        {
            string log_folder = "r:\\";
            DirectoryInfo dirInfo = new DirectoryInfo(log_folder);
            if (dirInfo.Exists)
            {

            }
            else
            {
                log_folder = AppDomain.CurrentDomain.BaseDirectory + "log\\";
                if (!Directory.Exists(log_folder))//验证路径是否存在
                {
                    Directory.CreateDirectory(log_folder); //不存在则创建
                }
            }

            //string log_folder = AppDomain.CurrentDomain.BaseDirectory + "log\\";
            log_folder = log_folder.Replace("\\\\", "\\");

            string strLogFullPathName1 = string.Format("{0}log1.txt", log_folder);
            FileInfo fiLog1 = new FileInfo(strLogFullPathName1);

            StreamWriter sw;
            if (!fiLog1.Exists)   ////验证文件是否存在，有则追加，无则创建
            {
                sw = fiLog1.CreateText();
            }
            else
            {
                sw = fiLog1.AppendText();
            }
            sw.WriteLine("\r\n\r\n程序开始 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            sw.WriteLine("  ");
            sw.Close();
        }

        public static FileInfo Get_FileInfo_Log()
        {
            string log_folder = AppDomain.CurrentDomain.BaseDirectory + "log\\";
            string strLogFullPathName1 = log_folder + "log1.txt";
            //strLogFullPathName1 = strLogFullPathName1.Replace("\\\\", "\\");

            FileInfo fiLog1 = new FileInfo(strLogFullPathName1);
            if (fiLog1.Exists)   ////验证文件是否存在，有则 检查 大小，过大则备份
            {
                if (fiLog1.Length > 2048000)
                {
                    string strLogFullPathName5 = log_folder + "log5.txt";
                    FileInfo fiLog5 = new FileInfo(strLogFullPathName5);
                    if (fiLog5.Exists)
                    {
                        fiLog5.Delete();
                    }

                    string strLogFullPathName4 = log_folder + "log4.txt";
                    FileInfo fiLog4 = new FileInfo(strLogFullPathName4);
                    if (fiLog4.Exists)
                    {
                        fiLog4.MoveTo(strLogFullPathName5);
                    }

                    string strLogFullPathName3 = log_folder + "log3.txt";
                    FileInfo fiLog3 = new FileInfo(strLogFullPathName3);
                    if (fiLog3.Exists)
                    {
                        fiLog3.MoveTo(strLogFullPathName4);
                    }

                    string strLogFullPathName2 = log_folder + "log2.txt";
                    FileInfo fiLog2 = new FileInfo(strLogFullPathName2);
                    if (fiLog2.Exists)
                    {
                        fiLog2.MoveTo(strLogFullPathName3);
                    }

                    //string strLogFullPathName1 = string.Format("{0}\\log1.txt", log_folder);
                    FileInfo fiLog11 = new FileInfo(strLogFullPathName1);
                    if (fiLog11.Exists)
                    {
                        fiLog11.MoveTo(strLogFullPathName2);
                    }


                }
            }

            if (!fiLog1.Exists)   ////验证文件是否存在，有则追加，无则创建
            {
                StreamWriter sw;
                sw = fiLog1.CreateText();
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                sw.Close();
            }

            return fiLog1;
        }

        public static void WriteLog(string strLog)
        {
            FileInfo fiLog1 = Get_FileInfo_Log();
            StreamWriter sw = fiLog1.AppendText();
            sw.WriteLine(strLog);
            sw.Close();
        }

        public static void WriteLogTime(string strLog)
        {
            FileInfo fiLog1 = Get_FileInfo_Log();
            StreamWriter sw = fiLog1.AppendText();
            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "   ---   " + strLog);
            sw.Close();
        }

        //public static void writelog(Exception ex)
        //{
        //    FileInfo fiLog1 = Get_FileInfo_Log();
        //    StreamWriter sw = fiLog1.AppendText();
        //    sw.WriteLine("-----------------------------------------------");
        //    sw.WriteLine("date:" + DateTime.Now.ToShortDateString() + " time" + DateTime.Now.ToShortTimeString());
        //    sw.WriteLine(ex.Message);
        //    sw.WriteLine(ex.StackTrace);
        //    sw.Close();
        //}


        /// <summary>
        /// 将异常打印到LOG文件
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="LogAddress">日志文件地址</param>
        public static void WriteLog(Exception ex)
        {
            FileInfo fiLog1 = Get_FileInfo_Log();
            StreamWriter sw = fiLog1.AppendText();
            sw.WriteLine("-----------------------------------------------");
            sw.WriteLine("当前时间：" + DateTime.Now.ToString());
            sw.WriteLine("异常信息：" + ex.Message);
            sw.WriteLine("异常对象：" + ex.Source);
            sw.WriteLine("调用堆栈：\n" + ex.StackTrace.Trim());
            sw.WriteLine("触发方法：" + ex.TargetSite);
            sw.WriteLine();
            sw.Close();
        }



    }
}
